package com.ilop.sthome.ui.activity.scene;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.example.common.view.WheelView;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySetTempControlBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020-02-04.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置温控器输出动作（执行动作）
 */
public class SettingTempControlActivity extends BaseBindActivity<ActivitySetTempControlBinding> {
    private boolean isAdd;
    private boolean isUpdate;
    private DeviceInfoBean mDevice;
    private AutomationBean mAutoBean;

    private List<DeviceInfoBean> mDeviceList;
    private ArrayList<String> set_decimal;
    private ArrayList<String> items_temp;
    private ArrayList<String> items_min;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_temp_control;
    }

    @Override
    protected void initialize() {
        super.initialize();
        isUpdate = getIntent().getBooleanExtra(CommonId.KEY_UPDATE, false);
        mDevice = (DeviceInfoBean) getIntent().getSerializableExtra(CommonId.KEY_DEVICE);
        isAdd = getIntent().getBooleanExtra(CommonId.KEY_ADD_NEW, false);
        mAutoBean = (AutomationBean) getIntent().getSerializableExtra(CommonId.KEY_AUTOMATION);
    }

    @Override
    protected void initView() {
        super.initView();
        String timer_dw = "(" + getString(R.string.m_s) + ")";
        mDBind.setTimeDw.setText(timer_dw);
        mDeviceList = new ArrayList<>();
        items_temp = new ArrayList<>();
        set_decimal = new ArrayList<>();
        items_min = new ArrayList<>();
        mDBind.actionMinute.setCyclic(true);
        mDBind.actionSecond.setCyclic(true);
    }

    @Override
    protected void initData() {
        super.initData();
        set_decimal.add("0");
        set_decimal.add("5");
        for (int i = 5; i < 31; i++) {
            String item = String.valueOf(i);
            items_temp.add(item);
        }
        for (int i = 0; i < 60; i ++) {
            String item = String.valueOf(i);
            if (item.length() == 1) {
                item = "0" + item;
            }
            items_min.add(item);
        }
        mDBind.setTemp.setAdapter(new NumberAdapter(items_temp));
        mDBind.setTempTwo.setAdapter(new NumberAdapter(set_decimal));
        mDBind.actionMinute.setAdapter(new NumberAdapter(items_min));
        mDBind.actionSecond.setAdapter(new NumberAdapter(items_min));
        if (isUpdate){
            String status = mDevice.getDevice_status();
            String type = status.substring(0,2);
            if ("82".equals(type)){
                showTemp(status);
            }else if ("84".equals(type)){
                showTemp(status);
                int minute = Integer.parseInt(status.substring(6, 8), 16);
                int second = Integer.parseInt(status.substring(8), 16);
                mDBind.actionMinute.setCurrentItem(minute);
                mDBind.actionSecond.setCurrentItem(second);
            }
            mDBind.controlDelete.setVisibility(View.VISIBLE);
        }
    }

    private void showTemp(String status){
        int code = Integer.parseInt(status.substring(2, 4), 16);
        if (code>30){
            int temp = code - 32;
            mDBind.setTemp.setCurrentItem(temp-5);
            mDBind.setTempTwo.setCurrentItem(1);
        }else {
            mDBind.setTemp.setCurrentItem(code-5);
            mDBind.setTempTwo.setCurrentItem(0);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivBack.setOnClickListener(v -> finish());
        mDBind.tvSaveTemp.setOnClickListener(v -> {
            int minute = mDBind.actionMinute.getCurrentItem();
            int second = mDBind.actionSecond.getCurrentItem();

            int temp = mDBind.setTemp.getCurrentItem() + 5;
            int tempDecimal = mDBind.setTempTwo.getCurrentItem() == 1 ? 32 : 0;
            String setting_temp = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(temp + tempDecimal));

            if (minute != 0 || second != 0) {
                String m = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(minute));
                String s = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(second));
                String delay = m + s;
                mDevice.setBinVersion(delay);
                mDevice.setDevice_status("84" + setting_temp + "80" + delay);
            }else {
                mDevice.setDevice_status("82" + setting_temp + "80");
            }
            mDeviceList.add(mDevice);
            if (isAdd){
                mAutoBean.setOutputList(mDeviceList);
                Intent intent = new Intent();
                intent.setClass(mContext, AutomationDetailActivity.class);
                intent.putExtra(CommonId.KEY_ADD_SCENE, true);
                intent.putExtra(CommonId.KEY_AUTOMATION, mAutoBean);
                intent.putExtra(CommonId.KEY_DEVICE_NAME, mDevice.getDeviceName());
                startActivity(intent);
            }else {
                if (!isUpdate) {
                    LiveDataBus.get().with(CommonId.OUTPUT_CONDITION).setValue(mDevice);
                } else {
                    LiveDataBus.get().with(CommonId.OUTPUT_UPDATE).setValue(mDevice);
                }
            }
            finish();
        });

        mDBind.setTemp.addScrollingListener(new WheelView.OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                if (wheel.getCurrentItem() == 25){
                    mDBind.setTempTwo.setCurrentItem(0);
                }
            }
        });

        mDBind.setTempTwo.addScrollingListener(new WheelView.OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                if (wheel.getCurrentItem() == 1 && mDBind.setTemp.getCurrentItem() == 25){
                    mDBind.setTempTwo.setCurrentItem(0);
                }
            }
        });
        mDBind.controlDelete.setOnClickListener(view -> {
            LiveDataBus.get().with(CommonId.DELETE_CONDITION).setValue(false);
            finish();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(mDevice.getSubdeviceName())) {
            String title = mContext.getString(SmartDevice.getType(mDevice.getDevice_type()).getTypeStrId()) + mDevice.getDevice_ID();
            mDBind.tvTempTitle.setText(title);
        } else {
            mDBind.tvTempTitle.setText(mDevice.getSubdeviceName());
        }
    }

    private static class NumberAdapter extends WheelView.WheelArrayAdapter<String> {
        NumberAdapter(ArrayList<String> items) {
            super(items);
        }
    }
}
